/* 
 * File:   SpaceShip.cpp
 * Author: brunoslr
 * 
 * Created on 16 de Setembro de 2009, 00:07
 */

#include "spaceship.h"

SpaceShip::SpaceShip(GLint tex_player){

    positionx = WIDTH/2;
    positiony = BOTTOM;
    radius = LARGURAJOGADOR/2;
    expFrame = 0;
    explosion = false;
    gameOver = false;
    speed = 0.0;
    score = 0;
    lifes = 3;
         
    for(int i=0; i<MaxShoots; i++){
       tiro[i]= new Shoot();
       }
   
    drawSpaceship(tex_player);


}

SpaceShip::SpaceShip(const SpaceShip& orig) {
}

SpaceShip::~SpaceShip() {
}

double SpaceShip::getPositionx(){
    return positionx;
}

double SpaceShip::getPositiony(){
    return positiony;
}

void SpaceShip::setSpeed(double x){
     this->speed = x;
}

double SpaceShip::getSpeed(){
    return speed;
}

int SpaceShip::drawSpaceship(GLint tex_player) {
    if(gameOver==false||explosion==true){
        glEnable(GL_TEXTURE_2D);
        glEnable(GL_ALPHA_TEST);
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
        glBindTexture(GL_TEXTURE_2D, tex_player);
        glBegin(GL_QUADS);
        glColor3f(1.0,1.0,1.0);
        glTexCoord2d(0.0, 1.0);
        glVertex2f(positionx - LARGURAJOGADOR/2, positiony-ALTURAJOGADOR/2);
        glTexCoord2d(0.0, 0.0);
        glVertex2f(positionx - LARGURAJOGADOR/2, positiony + ALTURAJOGADOR/2);
        glTexCoord2d(1.0, 0.0);
        glVertex2f(positionx + LARGURAJOGADOR/2, positiony+ALTURAJOGADOR/2);
        glTexCoord2d(1.0, 1.0);
        glVertex2f(positionx + LARGURAJOGADOR/2, positiony-ALTURAJOGADOR/2);
        glEnd();
        glDisable(GL_TEXTURE_2D);
        glDisable(GL_BLEND);
        glClear(GL_COLOR);


        for(int i=0; i<MaxShoots; i++){
            if(this->tiro[i]->status == true){
                tiro[i]->drawShoot(LARGURAJOGADOR,-1);
            }
        }
    }
    return 0;
}

int SpaceShip::move() {

    if((positionx < WIDTH*0.96 & speed > 0 ) || ( positionx > WIDTH*0.04 & speed < 0)){
    positionx+=speed;

    }
     
    for(int i=0; i<MaxShoots; i++){
        if(this->tiro[i]->status == true){
            tiro[i]->move(0.20, 1);
        }
    }

    return 0;
}

int SpaceShip::TryToShoot(int x) {

    for(int i=0; i<x; i++){
        if(this->tiro[i]->status == false){
            this->tiro[i] = new Shoot(this->getPositionx(), this->getPositiony(), LARGURAJOGADOR/2, 1);
            return 0;
        }
    } return 0;
}

int SpaceShip::CollisionTestShoots(double x, double y){
  for(int i=0; i<MaxShoots; i++){
       if(this->tiro[i]->status == true){
            if(this->tiro[i]->collisionTest(ENEMYWIDTH/2, x,y)){
                this->tiro[i]->status=false;
                return 1;
            }
        }
    }return 0;
}

int SpaceShip::destroyed(){
    this->lifes--;
    this->explosion=true;
    this->undestructible=50;
    this->expFrame=0;

    if(this->lifes<=0){
        this->gameOver=true;
        return 1;
    }
    return 0;
}
